package jp.wasabeef.glide.transformations;

/**
 * Copyright (C) 2020 Wasabeef
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import jp.wasabeef.glide.transformations.utility.Utils;
import org.jetbrains.annotations.NonNls;

import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;

import java.security.MessageDigest;
import java.util.Optional;

public class MaskTransformation extends BitmapTransformation {
  private static final int VERSION = 1;
  private static final String ID = "jp.wasabeef.glide.transformations.MaskTransformation." + VERSION;

  private static final Paint paint = new Paint();
  private final int maskId;
  private Context context;

//  static {
//    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//  }

  /**
   * @param maskId If you change the mask file, please also rename the mask file, or Glide will get
   *               the cache with the old mask. Because key() return the same values if using the
   *               same make file name. If you have a good idea please tell us, thanks.
   */
  public MaskTransformation(Context context, int maskId) {
    this.maskId = maskId;
    this.context = context;
  }

  @Override
  protected PixelMap transform(@NonNls BitmapPool pool, @NonNls PixelMap toTransform, int outWidth, int outHeight) {
    int width = toTransform.getImageInfo().size.width;
    int height = toTransform.getImageInfo().size.height;

    PixelFormat config =
            toTransform.getImageInfo() != null ? toTransform.getImageInfo().pixelFormat : PixelFormat.ARGB_8888;
    PixelMap bitmap = pool.get(width, height, config);

    setCanvasBitmapDensity(toTransform, bitmap);
    bitmap.setAlphaType(AlphaType.PREMUL);

//    Drawable mask = context.getDrawable(maskId);
//    PixelMapElement pixelMapElement = new PixelMapElement(ResourceTable);
    Optional<PixelMap> optional = Utils.getPixelMap(context, maskId);
    PixelMapElement pixelMapElement = optional.map(PixelMapElement::new).orElse(null);

    setCanvasBitmapDensity(toTransform, bitmap);

    Canvas canvas = new Canvas(new Texture(bitmap));
    pixelMapElement.setBounds(0, 0, width, height);
    pixelMapElement.drawToCanvas(canvas);
    canvas.drawPixelMapHolder(new PixelMapHolder(toTransform), 0, 0, paint);

    return bitmap;
  }

  @Override
  public String toString() {
    return "MaskTransformation(maskId=" + maskId + ")";
  }

  @Override
  public boolean equals(Object o) {
    return o instanceof MaskTransformation &&
      ((MaskTransformation) o).maskId == maskId;
  }

  @Override
  public int hashCode() {
    return ID.hashCode() + maskId * 10;
  }

  @Override
  public void updateDiskCacheKey(@NonNls MessageDigest messageDigest) {
    messageDigest.update((ID + maskId).getBytes(CHARSET));
  }
}